package com.ebelter.seneo.ui.activitys;


import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserinfos;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.StatusBarUtil;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.adapters.MembersAdapter;
import com.ebelter.seneo.adapters.MembersAdapter2;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.models.SeneoDbUtils;
import com.ebelter.seneo.ui.views.AddNameDialog;
import com.ebelter.seneo.utils.SeneoUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import static com.ebelter.seneo.ui.activitys.SeneoMainActivity.white;

public class SeneoMembersListActivity extends SeneoBaseActivity {

    public static final String TAG = "SeneoMembersListActivity";
    ImageView top_iv;
    TextView top_tv1;

    ListView members_lv;
    MembersAdapter2 membersAdapter;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_members_list;
    }


    @Override
    protected void initView() {

        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
        StatusBarUtil.setColor(this,white,0);
        StatusBarUtil.setDarkStatusIcon(this,true);

        FV();

        membersAdapter=new MembersAdapter2(this);
        members_lv.setAdapter(membersAdapter);

        top_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //添加底部按钮
        View footView = View.inflate(this,R.layout.item_members_ly,null);
        ImageView iv1 = footView.findViewById(R.id.item_mem_iv1);
        ImageView iv2 = footView.findViewById(R.id.item_mem_iv2);
        TextView tv1 = footView.findViewById(R.id.item_mem_tv1);
        View v = footView.findViewById(R.id.item_mem_v);
        ViewUtils.hideView(v);
        ViewUtils.hideView(iv2);
        tv1.setText("Add");
        iv1.setImageResource(R.drawable.ic_add);

        footView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addUser();
            }
        });

        members_lv.addFooterView(footView);


        members_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SeneoUserinfos which=membersAdapter.getUsers().get(position);
                SeneoPeopleDetailInfoActivity.openSeneoPeopleDetailInfoActivity(SeneoMembersListActivity.this,which.user_id,0);
            }
        });
    }

    //添加用户
    private void addUser() {
        LogUtils.i(TAG2,"---addUser");

        AddNameDialog addNameDialog=new AddNameDialog(this);
        addNameDialog.show();
        addNameDialog.setListener(new AddNameDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                LogUtils.i(TAG2,"----关闭");
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView, String str) {
                LogUtils.i(TAG2,"----确定 str = " + str);
                //1 先验证昵称不可以为空和先查询一遍数据库有没有这个名字

                if (TextUtils.isEmpty(str)){
                    ToastUtil.show(R.string.Nikename_cannot_be_empty);
                    return;
                }
                if (str.length()>20){
                    ToastUtil.show(R.string.nikename_over_tip);
                    return;
                }
                if (membersAdapter!=null){
                    boolean isContain = membersAdapter.isContain(str);
                    if (isContain){
                        //ToastUtil.show("The nickname has been registered, please use another one.");
                        ToastUtil.show("Nickname already exists.");
                        return;
                    }
                    long hostId = SeneoDbUtils.getHostId();
                    SeneoAddInfoActivity.openSeneoAddInfoActivity(SeneoMembersListActivity.this,1001,SeneoAddInfoActivity.CMD_register_children,str,hostId);
                    dialogView.cancel();
                }



            }
        });



    }

    private void FV() {

        top_iv=findViewById(R.id.top_iv);
        top_tv1=findViewById(R.id.top_tv1);

        ViewUtils.displayView(top_tv1);
        ViewUtils.setTextViewStr(top_tv1,"Members");

        members_lv=findViewById(R.id.members_lv);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CommonEventBus event) {
        if (!event.whichReceive.contains(TAG)) {
            return;
        }
        LogUtils.i(TAG, TAG+"--onEventMainThread接收到CommonEventBus信息--" + event.toString());
        if (event.cmd== IUserContant.ICMDS.UPDATE_CURRENT_USER_INFO){
            LogUtils.i(TAG, TAG+"--onEventMainThread更新当前用户信息");
            if (membersAdapter!=null)membersAdapter.notifyDataSetChanged2();
        }else if (event.cmd== IUserContant.ICMDS.DELETE_User_SUCCESS){
            LogUtils.i(TAG, TAG+"--onEventMainThread删除用户成功");
            long userid = (long) event.getContent();
            if (membersAdapter!=null)membersAdapter.removeUser(userid);
        }
    }


    private Handler handler=new Handler();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (membersAdapter!=null){
            membersAdapter.notifyDataSetChanged2();


            if (resultCode==RESULT_OK){
                LogUtils.i2(TAG,"添加用户成功");
                long userid=data.getLongExtra("SELECT_USERID",-1);
                LogUtils.i2(TAG,"添加用户成功 userid = "+userid);
                if (userid>0){
                    CommonEventBus commonEventBus=new CommonEventBus(TAG,SeneoMainActivity.TAG, IUserContant.ICMDS.SWITCH_USER,"切换用户");
                    commonEventBus.setContent(userid);
                    EventBus.getDefault().post(commonEventBus);
                    finish();

                }
            }



        }
    }
}
